package com.wang.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wang.dao.mapper.ArticleMapper;
import com.wang.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class ThreadService {

    @Autowired
    private ArticleService articleService;

    @Async("taskExecutor")
    public void updateArticleViewCount(ArticleMapper articleMapper, Article article) {
        Integer viewCounts = article.getViewCounts();
        Article articleupdate = new Article();
        articleupdate.setViewCounts(viewCounts + 1);

        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getId, article.getId());
        queryWrapper.eq(Article::getViewCounts, viewCounts);

        articleMapper.update(articleupdate, queryWrapper);
        try {
            Thread.sleep(5000);
            System.out.println("更新完成");
        }catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    @Async("taskExecutor")
    public void updateArticleCommentCount(Long articleId) {
        Article article = articleService.getArticleById(articleId);
        article.setCommentCounts(article.getCommentCounts() + 1);
        articleService.updateById(article);
        try {
            Thread.sleep(5000);
            System.out.println("更新完成");
        }catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
